package com.iot.service.mqtt.codec.encoder;

import com.iot.service.mqtt.model.fix.TlMqttFixedHead;
import com.iot.service.mqtt.model.response.TlMqttUnSubAck;
import com.iot.service.mqtt.model.variable.TlMqttUnSubAckVariableHead;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @Author: hszhou
 * @Date: 2024/11/30 15:12
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@ChannelHandler.Sharable
@Component
public class TlMqttUnSubAckEncoder extends MessageToByteEncoder<TlMqttUnSubAck> {


    private static final Logger logger = LoggerFactory.getLogger(TlMqttUnSubAckEncoder.class);

    @Override
    protected void encode(ChannelHandlerContext ctx, TlMqttUnSubAck req, ByteBuf out) throws Exception {
        logger.debug("in【PUBREC】encode");

        TlMqttUnSubAckVariableHead variableHead = req.getVariableHead();
        TlMqttFixedHead fixedHead = req.getFixedHead();

        int type = fixedHead.getMessageType().value() << 4;
        //消息类型
        out.writeByte(type);
        //剩余长度
        out.writeByte(Short.BYTES);
        out.writeShort(variableHead.getMessageId());

    }
}